package com.qlylkj.lbk.network

import android.content.Context
import android.os.Build
import com.forest.core.etx.connectivityManager
import com.jeremyliao.liveeventbus.LiveEventBus
import com.qlylkj.lbk.model.event.LiveEvent
import com.forest.core.util.NetWorkUtil
import com.qlylkj.lbk.app.PreferenceHelper

/**
 *    author : duanguosen
 *    date   : 2019/12/5
 *    desc   :
 */
object NetworkHelper {

    fun initNetWorkCallback(context: Context){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            NetWorkUtil.registerNetworkCallback(context,
                object : NetWorkUtil.NetworkChangeListener {
                    override fun onNetworkChange(networkStatus: String) {
                        when (networkStatus) {
                            NetWorkUtil.NETWORK_AVAILABLE -> {
                                if(!PreferenceHelper.hasNetwork){
                                    LiveEventBus.get(LiveEvent.NETWORK_CHANGE_KEY).post(true)
                                }
                            }
                            NetWorkUtil.NETWORK_CONNECTED -> {
                                val isConnected = context.connectivityManager?.activeNetworkInfo?.isConnected ?: false
                                if(!isConnected){
                                    LiveEventBus.get(LiveEvent.NETWORK_CHANGE_KEY).post(false)
                                }
                            }
                            NetWorkUtil.NETWORK_WIFI -> {

                            }
                            NetWorkUtil.NETWORK_MOBILE -> {

                            }
                            else -> {

                            }
                        }
                    }
                })
        }
    }
}